package config

import (
	"errors"
	"os"
)

type ServerConfig interface {
	GetServerAddr() string
	GetServerPort() string
	GetServerType() string
}

type serverConfig struct {
	serverAddr string `env:"SERVER_ADDR" envDefault:"localhost"`
	serverPort string `env:"SERVER_PORT" envDefault:":8080"`
	serverType string `env:"SERVER_TYPE" envDefault:"http"`
}

func NewServerConfig() (ServerConfig, error) {
	address := os.Getenv("SERVER_ADDR")
	if len(address) == 0 {
		return nil, errors.New("environment variable SERVER_ADDR is required")
	}
	port := os.Getenv("SERVER_PORT")
	if len(port) == 0 {
		return nil, errors.New("environment variable SERVER_PORT is required")
	}
	serverType := os.Getenv("SERVER_TYPE")
	if len(serverType) == 0 {
		return nil, errors.New("environment variable SERVER_TYPE is required")
	}
	return &serverConfig{
		serverAddr: address,
		serverPort: port,
		serverType: serverType,
	}, nil
}

func (s *serverConfig) GetServerAddr() string {
	return s.serverAddr
}

func (s *serverConfig) GetServerPort() string {
	return s.serverPort
}

func (s *serverConfig) GetServerType() string {
	return s.serverType
}
